package com.ddwl.user.controller;

/**
 * 类ProfitController.java的实现描述：
 *
 * @author zach
 * @Date :2020-06-29 11:11.
 */

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.constant.DistConstant;
import com.ddwl.schema.bo.user.SetDistrConfigBo;
import com.ddwl.schema.bo.user.SetUserAgentRegionBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.dist.DistrConfigVo;
import com.ddwl.schema.vo.user.UserVo;
import com.ddwl.user.service.ProfitApplyServer;
import com.ddwl.user.service.UserService;
import com.ddwl.user.service.DistrConfigServer;
import com.ddwl.user.service.ProfitRegionServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 类ProfitController.java的实现描述：分销代理接口
 *
 * @author zach
 * @Date :2020-06-24 15:00.
 */
@Api(tags = "分销代理接口")
@RequestMapping("/profit")
@RestController
public class ProfitController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private DistrConfigServer distrConfigServer;
    @Resource
    private ProfitRegionServer profitRegionServer;
    @Resource
    private ProfitApplyServer profitApplyServer;

    @PostMapping("/distSource")
    @ApiOperation("获取用户的分消源数据")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    public RestVo<String> getDistSource(){
        String pk=getAuthMap().get("pk");
        if (StrUtil.isNotEmpty(pk) && pk.equals(BaseEnum.ProductEnum.FENXIAO.getName())) {
            return RestVo.ok(getUserId());
        }
        return RestVo.ok(profitApplyServer.getFXUser(getUserId()));
    }

    @PostMapping("/appletSource")
    @ApiOperation("小程序获取源")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<String> appletSource(){
        return RestVo.ok(getUserId());
    }

    @GetMapping("/config")
    @ApiOperation("获取分销配置信息")
    //@Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.API})
    public RestVo<DistrConfigVo> getDistConfig() {
        return RestVo.ok(distrConfigServer.getDistrConfig(DistConstant.DistrConfigType.PRODUCT.getCode()));
    }


    @GetMapping("/config/{type}")
    @ApiOperation("获取分销配置信息")
    //@Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.API})
    public RestVo<DistrConfigVo> getDistConfig(@PathVariable("type") Integer type) {
        return RestVo.ok(distrConfigServer.getDistrConfig(type));
    }

    @PostMapping("/config")
    @ApiOperation("设置分销配置信息")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo getDistConfig(@Valid @RequestBody SetDistrConfigBo bo) {
        distrConfigServer.setConfig(bo);
        return RestVo.ok();
    }

    @PostMapping("/agentRegion")
    @ApiOperation("设置用户代理的区域")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo setUserAgentRegion(@Valid @RequestBody SetUserAgentRegionBo agentRegion){
        profitRegionServer.setUserAgentRegion(agentRegion);
        return RestVo.ok();
    }

    @GetMapping("/{userId}/agentRegion")
    @ApiOperation("查询用户代理的区域")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.API})
    public RestVo<List<String>> getUserAgentRegion(@PathVariable("userId") String userId){
        return RestVo.ok(profitRegionServer.getUserRegion(userId));
    }

    @GetMapping("/agentRegion")
    @ApiOperation("查询被代理的区域")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<List<String>> getAgentRegion(){
        return RestVo.ok(profitRegionServer.getAgentRegion());
    }

    @GetMapping("/{userId}/profile")
    @ApiOperation(value = "根据源信息获取用户数据",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserVo> getProfile(@PathVariable("userId") String userId) {
        return RestVo.ok(userService.getById(userId));
    }

    @GetMapping("/{phone}/fenxiao")
    @ApiOperation(value = "根据电话获取用户数据",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserVo> getProfileByPhone(@PathVariable("phone") String phone) {
        return RestVo.ok(userService.getByPhone(phone));
    }

    @GetMapping("/fenxiao/userInfo")
    @ApiOperation(value = "获取五八马用户的分销用户信息")
    @Auth(required = false)
    public RestVo<UserVo> getWbmFenxiao(@RequestParam("userId") String userId){
        return RestVo.ok(userService.getByUserId(userId));
    }

    @GetMapping("/{region}/user")
    @ApiOperation(value = "查询区域合伙人的用户信息",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserVo> getRegionUser(@PathVariable("region") String region) {
        return RestVo.ok(profitRegionServer.getRegionUser(region));
    }

    /**
     * 根据区域id查询该区域的总代理
     * @param regionId
     * @return
     */
    @GetMapping("/getTopUserByRegionId")
    @ApiOperation(value = "根据区域id查询该区域的总代理",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserVo> getTopUserByRegionId(@RequestParam("regionId") String regionId){
        return RestVo.ok(profitRegionServer.getTopUserByRegionId(regionId));
    }
}
